using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;

namespace UTorrentLib
{
    [Flags]
    public enum UTorrentStatus
    {
        Started = 1,
        Checking = 2,
        StartAfterCheck = 4,
        Checked = 8,
        Error = 16,
        Paused = 32,
        Queued = 64,
        Loaded = 128
    }
    public class UTorrent
    {

        private TorrentSession m_sParent;
        public JavaScriptArray m_jsTorrent;
        public UTorrentFileList m_Files = new UTorrentFileList();
        public String Hash
        {
            get
            {
                return (String)m_jsTorrent[0];
            }
        }
        public UTorrentStatus Status
        {
            get
            {
                return (UTorrentStatus)((long)m_jsTorrent[1]);
            }
        }
        public String Name
        {
            get
            {
                return (String)m_jsTorrent[2];
            }
        }
        public long Size
        {
            get
            {
                return (long)m_jsTorrent[3];
            }
        }
        public int Progress
        {
            get
            {
                return (int)Math.Floor((double)((long)m_jsTorrent[4] / 10));
            }
        }
        public long Downloaded
        {
            get
            {
                return (long)m_jsTorrent[5];
            }
        }
        public long Uploaded
        {
            get
            {
                return (long)m_jsTorrent[6];
            }
        }
        public long Ratio
        {
            get
            {
                return (long)m_jsTorrent[7];
            }
        }
        public long UploadSpeed
        {
            get
            {
                return (long)m_jsTorrent[8];
            }
        }
        public long DownloadSpeed
        {
            get
            {
                return (long)m_jsTorrent[9];
            }
        }
        public long ETA
        {
            get
            {
                return (long)m_jsTorrent[10];
            }
        }
        public String Label
        {
            get
            {
                return (String)m_jsTorrent[11];
            }
        }
        public long PeersConnected
        {
            get
            {
                return (long)m_jsTorrent[12];
            }
        }
        public long PeersInSwarm
        {
            get
            {
                return (long)m_jsTorrent[13];
            }
        }
        public long SeedsConnected
        {
            get
            {
                return (long)m_jsTorrent[14];
            }
        }
        public long SeedsInSwarm
        {
            get
            {
                return (long)m_jsTorrent[15];
            }
        }
        public long Availability
        {
            get
            {
                return (long)m_jsTorrent[16];
            }
        }
        public long TorrentQueueOrder
        {
            get
            {
                return (long)m_jsTorrent[17];
            }
        }
        public long Remaining
        {
            get
            {
                return (long)m_jsTorrent[18];
            }
        }

        #region formated Strings for displaying in a GUI.
        public String fSize
        {
            get
            {
                return DisplayFunctions.FormatSize(Size);
            }
        }
        public String fDownloadSpeed
        {
            get
            {
                return DisplayFunctions.FormatSize(DownloadSpeed) + "/s";
            }
        }
        public String fUploadSpeed
        {
            get
            {
                return DisplayFunctions.FormatSize(UploadSpeed) + "/s";
            }
        }
        public String fSeeds
        {
            get
            {
                return SeedsConnected.ToString() + "(" + SeedsInSwarm.ToString() + ")";
            }
        }
        public String fPeers
        {
            get
            {
                return PeersConnected.ToString() + "(" + PeersInSwarm.ToString() + ")";
            }
        }
        public String fETA
        {
            get
            {
                if (ETA != -1)
                    return TimeSpan.FromSeconds((double)ETA).ToString();
                else
                    return String.Empty;
            }
        }
        #endregion

        public UTorrent(JavaScriptArray mArray,TorrentSession mParent)
        {
            m_jsTorrent = mArray;
            m_sParent = mParent;
        }

        public void Start()
        {
            m_sParent.GetResponseFromUi("?action=start&hash=" + this.Hash);
        }
        public void Stop()
        {
            m_sParent.GetResponseFromUi("?action=stop&hash=" + this.Hash);
        }
        public void Pause()
        {
            m_sParent.GetResponseFromUi("?action=pause&hash=" + this.Hash);
        }
        public void Remove(bool RemoveFiles)
        {
            String action = "?action=remove";
            if (RemoveFiles)
                action += "data";

            m_sParent.GetResponseFromUi(action + "&hash=" + this.Hash);
        }
        public void LoadFiles()
        {
            this.m_Files.Clear();
            JavaScriptObject GuiResp = m_sParent.GetResponseFromUi("?action=getfiles&hash=" + this.Hash);
            JavaScriptArray files = (JavaScriptArray)((JavaScriptArray)GuiResp["files"])[1];
            for (int i = 0; i < files.Count; i++)
            {
                this.m_Files.Add(new UTorrentFile((JavaScriptArray)files[i], this));
            }

        }
    }
}
